// UI组件管理类 （只管理plugins目录里插件相关的UI组件）
interface WidgetParam {
  name: string;
  styles?: string;
  comClass?: string;
  _props?: object;
  keyId: String
}

class WidgetsManager {
  widgets: any;
  constructor() {
    this.widgets = [];
  }

  init(widgets: any) {
    this.widgets = widgets;
  }

  // 激活指定UI组件
  activate(params: WidgetParam) {
    let index = this.hasActivateWidget(params.name);
    this.widgets.value = [];

    if (index > -1) {
      this.inactivate(params.name);
      this.widgets.value = [];
    }
    this.widgets.value.push({
      name: params.name,
      styles: params.styles,
      comClass: params.comClass,
      _props: params._props,
      keyId: params.keyId
    });
  }

  // 销毁指定UI组件
  inactivate(name: string) {
    let index = this.hasActivateWidget(name);
    if (index != -1) this.widgets.value.splice(index, 1);
  }

  // 销毁所有激活的组件
  inactivateAll() {
    this.widgets.value = [];
  }

  // 遍历当前激活的组件，返回是否存在指定name的组件(index>-1则存在)
  hasActivateWidget(name: string) {
    let index = -1;
    for (let i = 0; i < this.widgets.value.length; i++) {
      if (this.widgets.value[i].name === name) {
        index = i;
        return index;
      }
    }
    return index;
  }
}

export default WidgetsManager;
